<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>标签管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="queryInfo.query"
            clearable
            @clear="getTagList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getTagList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加标签</el-button
          >
        </el-col>
      </el-row>
      <!-- 标签列表区域 -->
      <el-table :data="blogTagList" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="标签">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.id % 5 === 1">{{ scope.row.name }}</el-tag>
            <el-tag type="success" v-else-if="scope.row.id % 5 === 2">{{
              scope.row.name
            }}</el-tag>
            <el-tag type="info" v-else-if="scope.row.id % 5 === 3">{{
              scope.row.name
            }}</el-tag>
            <el-tag type="warning" v-else-if="scope.row.id % 5 === 4">{{
              scope.row.name
            }}</el-tag>
            <el-tag type="danger" v-else>{{ scope.row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row.id)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeTagById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加文章标签的对话框 -->
    <el-dialog
      title="添加标签"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTag">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog
      title="修改标签名称"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <el-form-item label="标签id">
          <el-input v-model="editForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editTag">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      blogTagList: [],
      total: 0,
      tagType: ["primary", "success", "info", "warning", "danger"],
      addDialogVisible: false,
      addForm: {
        name: "",
      },
      // 添加表单的验证规则对象
      addFormRules: {
        name: [
          { required: true, message: "请输入文章标签名称", trigger: "blur" },
        ],
      },
      editDialogVisible: false,
      editForm: {},
      editFormRules: {
        name: [
          { required: true, message: "请输入文章标签名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getTagList();
  },
  computed: {
    // 随机选取标签
    getRandomTag: function () {
      return this.tagType[Math.floor(Math.random() * this.tagType.length)];
    },
  },
  methods: {
    async getTagList() {
      const { data: res } = await this.$http.get("tagByQuery", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 0) {
        return this.$message.error("获取文章标签列表失败");
      }
      this.blogTagList = res.data.tags;
      this.total = res.data.total;
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getTagList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getTagList();
    },
    // 监听添加对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    addTag() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起请求
        const { data: res } = await this.$http.post("tag", this.addForm);
        if (res.meta.status === 0) {
          this.$message.success("添加标签成功！");
        }
        this.addDialogVisible = false;
        this.getTagList();
      });
    },
    // 展示编辑对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get("tag/" + id);
      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    editTag() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "tag/" + this.editForm.id,
          this.editForm
        );
        if (res.meta.status === 0) {
          this.$message.success("修改标签名称成功！");
        }
        this.editDialogVisible = false;
        this.getTagList();
      });
    },
    async removeTagById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该标签, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete("tag/" + id);

      if (res.meta.status === 0) {
        this.$message.success("删除文章标签成功！");
      }
      this.getTagList();
    },
  },
};
</script>

<style lang="less" scoped>
</style>